//override ext function

Ext.data.Store.prototype.listeners = {
            exception:function(dataProxy, type, action, options, response, arg) {
            		alert(SERVER_ERROR);
            		
            }
}

Ext.Ajax.on('beforerequest', beforerequest, this);
Ext.Ajax.on('requestcomplete', requestcomplete, this);
Ext.Ajax.on('requestexception', requestexception, this);
	//划选 grid 数据
	if (!Ext.grid.GridView.prototype.templates) {   
   	 	Ext.grid.GridView.prototype.templates = {};   
	}   
	Ext.grid.GridView.prototype.templates.cell = new Ext.Template(   
	    '<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} x-selectable {css}" style="{style}" tabIndex="0" {cellAttr}>',   
	   '<div class="x-grid3-cell-inner x-grid3-col-{id}" {attr}>{value}</div>',   
  	 '</td>'  
	); 

function beforerequest(conn,  options){
	var cmp = Ext.getCmp(options.maskId);
	
		if(cmp!=null){
		var xtype = cmp.getXType()
		if(xtype=="grid"||xtype=="form"||xtype=="panel"){
			cmp.el.mask('请稍等....', 'x-mask-loading');  
		}else{
			
			cmp.disable();
		}
		
	}

}
Ext.form.TextField.prototype.listeners={
	'beforerender':function (){
		if(!this.allowBlank){
			this.label.addClass("notNull");
			if(!Ext.isEmpty(this.blankText) && this.blankText=="This field is required"){
				this.blankText=this.fieldLabel+defalutBlankText;
			}
		}
	 }
}
/*给下拉框增加 emptyText指定值*/
Ext.form.ComboBox.prototype.listeners={
	'beforerender':function (){	
		if(!this.allowBlank){
			this.label.addClass("notNull");
			
			if(!Ext.isEmpty(this.blankText) && this.blankText=="This field is required"){
				this.blankText=this.fieldLabel+defalutBlankText;
			}
		}
		var comb = this;		
		if(!Ext.isEmpty(comb.emptyText) && this.allowBlank){			
		 	//这种用于从后台load数据
		 	if (!Ext.isEmpty(this.autoAdd) && this.autoAdd && (!Ext.isEmpty(this.esopLoadModel) && this.esopLoadModel=="remote") ){
				this.getStore().load({
					callback:function(a,b){
						CG.util.addEmptyText(comb);
					}
				});
		    	
	    	}
	    	else if(!Ext.isEmpty(this.autoAdd) && this.autoAdd && (!Ext.isEmpty(this.esopLoadModel) && this.esopLoadModel=="local")){
	    		CG.util.addEmptyText(comb);
	    	}
    	}
	 }
}
Ext.form.CheckboxGroup.prototype.listeners={
	'beforerender':function (){
	if(!this.allowBlank){
		this.label.addClass("notNull");
	}
 }
}

function requestcomplete( conn,  response,  options){
	var cmp = Ext.getCmp(options.maskId);	
	if(cmp!=null){
		var xtype = cmp.getXType()
		if(xtype=="grid"||xtype=="form"||xtype=="panel"){
			cmp.el.unmask('请稍等....', 'x-mask-loading');  
		}else{
			cmp.enable();
		}
	}
	
}

function requestexception( conn,  response,  options){
	requestcomplete(conn,response,options);
	
}


/*
tableform

var form = new Ext.form.FormPanel({
	renderTo:'content',
	layout:'tableform',
	width:600,
	frame:true,
	title:'test',
	layoutConfig: { columns: 2 },
                    items: [
                        { fieldLabel: 'Created By:', xtype: 'textfield'
                        },
                        { fieldLabel: 'Author:', xtype: 'textfield'
                        },
                       
                        { fieldLabel: 'Creation Date:', xtype: 'datefield' },
                        { fieldLabel: 'to', xtype: 'datefield' },
                        { fieldLabel: 'Modified By', xtype: 'textfield',colspan: 2},
                        { fieldLabel: 'Modified Date:', xtype: 'datefield' },
                        { fieldLabel: 'to', xtype: 'datefield' },
                     
	

})
*/


Ext.namespace("Ext.ux.layout");

Ext.ux.layout.TableFormLayout = Ext.extend(Ext.layout.TableLayout, {
    monitorResize: true,
    setContainer: function() {
        Ext.layout.FormLayout.prototype.setContainer.apply(this, arguments);
        this.currentRow = 0;
        this.currentColumn = 0;
        this.cells = [];
    },
    renderItem : function(c, position, target) {
        if (c && !c.rendered) {
            var cell = Ext.get(this.getNextCell(c));
            cell.addClass("x-stable-layout-column-" + this.currentColumn);
            Ext.layout.FormLayout.prototype.renderItem.call(this, c, 0, cell);
        }
    },
    getLayoutTargetSize : Ext.layout.AnchorLayout.prototype.getLayoutTargetSize,
    getTemplateArgs : Ext.layout.FormLayout.prototype.getTemplateArgs,
    onLayout : function(ct, target) {
        Ext.ux.layout.TableFormLayout.superclass.onLayout.call(this, ct, target);
        if (!target.hasClass("x-stable-form-layout-ct")) {
            target.addClass("x-stable-form-layout-ct");
        }
        var viewSize = this.getLayoutTargetSize(ct, target);
        var aw, ah;
        if (ct.anchorSize) {
            if (typeof ct.anchorSize == "number") {
                aw = ct.anchorSize;
            } else {
                aw = ct.anchorSize.width;
                ah = ct.anchorSize.height;
            }
        } else {
            aw = ct.initialConfig.width;
            ah = ct.initialConfig.height;
        }

        var cs = this.getRenderedItems(ct), len = cs.length, i, c, a, cw, ch, el, vs, boxes = [];
        var x, w, h, col, colWidth, offset;
        for (i = 0; i < len; i++) {
            c = cs[i];
           
            // get table cell
            x = c.getEl().parent(".x-table-layout-cell");
            if (this.columnWidths) {
                // get column
                col = parseInt(x.dom.className.replace(/.*x\-table\-layout\-column\-([\d]+).*/, "$1"));
                // get cell width (based on column widths)
                colWidth = 0, offset = 0;
                for (j = col; j < (col + (c.colspan || 1)); j++) {
                    colWidth += this.columnWidths[j];
                    offset += 10;
                }
                w = (viewSize.width * colWidth) - offset;
                
            } else {
                // get cell width
                
                w = (viewSize.width / this.columns) * (c.colspan || 1);
                //alert(w+'--'+this.columns+'---'+c.colspan);
               
            }
            // set table cell width
            x.setWidth(w);
            // get cell width (-10 for spacing between cells) & height to be base width of anchored component
            w = w - 10;
            h = x.getHeight();
            // If a child container item has no anchor and no specific width, set the child to the default anchor size
            if (!c.anchor && c.items && !Ext.isNumber(c.width) && !(Ext.isIE6 && Ext.isStrict)){
                c.anchor = this.defaultAnchor;
            }

            if(c.anchor){
                a = c.anchorSpec;
                if(!a){ // cache all anchor values
                    vs = c.anchor.split(' ');
                    c.anchorSpec = a = {
                        right: this.parseAnchor(vs[0], c.initialConfig.width, aw),
                        bottom: this.parseAnchor(vs[1], c.initialConfig.height, ah)
                    };
                }
                cw = a.right ? this.adjustWidthAnchor(a.right(w), c) : undefined;
                ch = a.bottom ? this.adjustHeightAnchor(a.bottom(h), c) : undefined;

                if(cw || ch){
                    boxes.push({
                        comp: c,
                        width: cw || undefined,
                        height: ch || undefined
                    });
                }
            }
        }      
        for (i = 0, len = boxes.length; i < len; i++) {
            c = boxes[i];
            c.comp.setSize(c.width, c.height);
        }      
    },
    
    parseAnchor : function(a, start, cstart) {
        if (a && a != "none") {
            var last;
            if (/^(r|right|b|bottom)$/i.test(a)) {
                var diff = cstart - start;
                return function(v) {
                    if (v !== last) {
                        last = v;
                        return v - diff;
                    }
                };
            } else if (a.indexOf("%") != -1) {
                var ratio = parseFloat(a.replace("%", "")) * .01;
                return function(v) {
                    if (v !== last) {
                        last = v;
                        return Math.floor(v * ratio);
                    }
                };
            } else {
                a = parseInt(a, 10);
                if (!isNaN(a)) {
                    return function(v) {
                        if (v !== last) {
                            last = v;
                            return v + a;
                        }
                    };
                }
            }
        }
        return false;
    },
    
    renderAll : function(ct, target){
        var items = ct.items.items, i, c, len = items.length;
        for(i = 0; i < len; i++) {
            c = items[i];
            if(c.xtype=="hidden"){
                continue;
            }
            if(c && (!c.rendered || !this.isValidParent(c, target))){
                this.renderItem(c, i, target);
            }
        }
    },
    
    

    adjustWidthAnchor : function(value, comp) {
        return value - (comp.isFormField ? (comp.hideLabel ? 0 : this.labelAdjust) : 0);
    },
    adjustHeightAnchor : function(value, comp) {
        return value;
    },
    // private
    isValidParent : function(c, target){
    	return c.getPositionEl().up('table', 6).dom.parentNode === (target.dom || target);
    },
    getLabelStyle : Ext.layout.FormLayout.prototype.getLabelStyle,
    labelSeparator : Ext.layout.FormLayout.prototype.labelSeparator,
    trackLabels: Ext.layout.FormLayout.prototype.trackLabels
});
Ext.Container.LAYOUTS['tableform'] = Ext.ux.layout.TableFormLayout;
Ext.apply(Ext.form.VTypes, {    
    myInteger:function(val,field){
    	return /^\d*$/.test(val);
    },
    myIntegerText:'必须是数字'
});

//showModelDialog可以复制
var g_rngAnchor = null;
			var g_wordSelect = false;
			var g_inMenu = false;
			function doSelection()
			{
				try {
				    var e = window.event;
				    e.cancelBubble = true;
				    e.returnValue = false;
				    // events.innerHTML = e.type + "-" + e.srcElement.tagName + " (" + e.clientX + ":" + e.clientY + ")<br/>" + events.innerHTML;
				    switch(e.type) {
				    case 'dblclick' :
				    	// Select a whole word.
				        g_rngAnchor = document.selection.createRange();
				    	if (g_rngAnchor.boundingWidth == 0) {
					        g_rngAnchor.moveToPoint(e.clientX, e.clientY);
					    	g_wordSelect = true;
				        }
			        	g_rngAnchor.moveStart("word",-1);
			        	g_rngAnchor.moveEnd("word");
			            g_rngAnchor.select();
			            break;
						
				    case 'mousedown' :
				    	// Set a selection point.
				        if (e.button == 1/*left*/ && !g_inMenu) {
				        	g_wordSelect = false;
					        document.selection.empty();
					        g_rngAnchor = document.selection.createRange();
					        g_rngAnchor.moveToPoint(e.clientX, e.clientY);
					        if (e.shiftKey) {
					        	g_rngAnchor.moveStart("word",-1);
					        	g_rngAnchor.moveEnd("word");
					        	g_wordSelect = true;
					        }
					    }
					    break;
				        
				    case 'mousemove' : 
				    	// Drag the selection. (SHIFT+drag to select words.)
				        if (e.button == 1 || g_wordSelect) {
				            var r2 = document.selection.createRange();
			            	r2.moveToPoint(e.clientX, e.clientY);
				            if (r2.compareEndPoints('StartToStart',g_rngAnchor) == -1) {
				                r2.setEndPoint('StartToEnd',g_rngAnchor);
				                if (e.shiftKey || g_wordSelect) r2.moveStart("word",-1);
				                r2.select();
				            }
				            else {
				                g_rngAnchor.setEndPoint('EndToEnd',r2);
				                if (e.shiftKey || g_wordSelect) g_rngAnchor.moveEnd("word");
				                g_rngAnchor.select();
				            }
				        }
				        break;
				        
				    case 'mouseup' :
				    	g_wordSelect = false;
				    }
				    // output.innerHTML = g_rngAnchor ? g_rngAnchor.boundingLeft + ":" + g_rngAnchor.boundingWidth : "null";
				}
				catch(e) {}
			}



